package com.mall.goods.service.impl;

import com.mall.common.constant.UserConstants;
import com.mall.common.core.domain.entity.SysDept;
import com.mall.common.utils.DateUtils;
import com.mall.common.utils.StringUtils;
import com.mall.goods.mapper.GoodsCategoryMapper;
import com.mall.goods.model.dto.GoodsCategoryDto;
import com.mall.goods.model.vo.GoodsCarouselVo;
import com.mall.goods.model.vo.GoodsCategoryVo;
import com.mall.goods.service.IGoodsCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsCategoryServiceImpl implements IGoodsCategoryService {

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 查询商城-商品分类
     */
    @Override
    public GoodsCategoryVo selectGoodsCategoryById(Long id)
    {
        return goodsCategoryMapper.selectGoodsCategoryById(id);
    }

    /**
     * 查询商城-商品分类列表
     */
    @Override
    public List<GoodsCategoryVo> selectGoodsCategoryList(GoodsCategoryDto goodsCategoryDto)
    {
        return goodsCategoryMapper.selectGoodsCategoryList(goodsCategoryDto);
    }

    /**
     * 新增商城-商品分类
     */
    @Override
    public int insertGoodsCategory(GoodsCategoryDto goodsCategoryDto)
    {
        goodsCategoryDto.setCreateTime(DateUtils.getNowDate());

        if (null != goodsCategoryDto.getParentId()) {
            GoodsCategoryVo info = goodsCategoryMapper.selectGoodsCategoryById(goodsCategoryDto.getParentId());
            if (null != info) {
                goodsCategoryDto.setAncestors(info.getAncestors() + "," + goodsCategoryDto.getParentId());
            } else {
                goodsCategoryDto.setAncestors("0");
            }
        } else {
            goodsCategoryDto.setAncestors("0");
        }

        return goodsCategoryMapper.insertGoodsCategory(goodsCategoryDto);
    }

    /**
     * 修改商城-商品分类
     */
    @Override
    public int updateGoodsCategory(GoodsCategoryDto goodsCategoryDto)
    {
        goodsCategoryDto.setUpdateTime(DateUtils.getNowDate());

        if (null != goodsCategoryDto.getParentId()) {
            GoodsCategoryVo newParentCategory = goodsCategoryMapper.selectGoodsCategoryById(goodsCategoryDto.getParentId());
            GoodsCategoryVo oldCategory = goodsCategoryMapper.selectGoodsCategoryById(goodsCategoryDto.getId());
            if (StringUtils.isNotNull(newParentCategory) && StringUtils.isNotNull(oldCategory))
            {
                String newAncestors = newParentCategory.getAncestors() + "," + newParentCategory.getId();
                String oldAncestors = oldCategory.getAncestors();
                goodsCategoryDto.setAncestors(newAncestors);
                updateCategoryChildren(goodsCategoryDto.getId(), newAncestors, oldAncestors);
            }
        }

        return goodsCategoryMapper.updateGoodsCategory(goodsCategoryDto);
    }

    /**
     * 修改子元素关系
     *
     * @param categoryId 被修改的部门ID
     * @param newAncestors 新的父ID集合
     * @param oldAncestors 旧的父ID集合
     */
    public void updateCategoryChildren(Long categoryId, String newAncestors, String oldAncestors)
    {
        List<GoodsCategoryVo> children = goodsCategoryMapper.selectChildrenCategoryById(categoryId);
        for (GoodsCategoryVo child : children)
        {
            child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
        }
        if (children.size() > 0)
        {
            List<GoodsCategoryDto> list = new ArrayList<>();
            children.forEach(child-> {
                GoodsCategoryDto goodsCategoryDto = new GoodsCategoryDto();
                goodsCategoryDto.setAncestors(child.getAncestors());
                goodsCategoryDto.setId(child.getId());
            });
            goodsCategoryMapper.updateCategoryChildren(list);
        }
    }

    /**
     * 批量删除商城-商品分类
     */
    @Override
    public int deleteGoodsCategoryByIds(Long[] ids)
    {
        return goodsCategoryMapper.deleteGoodsCategoryByIds(ids);
    }

    /**
     * 删除商城-商品分类信息
     */
    @Override
    public int deleteGoodsCategoryById(Long id)
    {
        return goodsCategoryMapper.deleteGoodsCategoryById(id);
    }
}
